package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yzy6.26exam/api/request"
	"yzy6.26exam/internal/bao"
	"yzy6.26exam/internal/moble"
)

func Witch(context *gin.Context) {
	var witch request.Witch
	if err := context.ShouldBind(&witch); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{
			"code":    204,
			"message": "解析失败",
			"data":    err.Error(),
		})
		return
	}
	var Witchs moble.Witch
	Witchs.UserId = int32(witch.Userid)
	Witchs.SeetId = int32(witch.SeedId)
	err := bao.FindOnce(&Witchs)
	if err != nil {
		context.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "关注失败",
			"data":    err.Error(),
		})
		return
	}
	if Witchs.Id == 0 {
		err := bao.Create(&Witchs)
		if err != nil {
			context.JSON(http.StatusOK, gin.H{
				"code":    200,
				"message": "关注失败",
				"data":    err.Error(),
			})
			return
		}
		context.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "关注成功",
		})
		return
	} else {
		err := bao.Delete(&Witchs)
		if err != nil {
			context.JSON(http.StatusOK, gin.H{
				"code":    200,
				"message": "取消关注失败",
				"data":    err.Error(),
			})
			return
		}
		context.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "取消关注成功",
		})
		return
	}
}
